package com.concurrent.frame.pojo;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class DelayVo<T> implements Delayed {
    private long activeTime;//到期时间,单位纳秒
    private T data;

    public DelayVo(long activeTime, T date) {
        //传入时间转换为超时时间,转换为: 纳秒
        this.activeTime = TimeUnit.NANOSECONDS.convert(activeTime,TimeUnit.MILLISECONDS)+System.nanoTime();
        this.data = date;
    }

    public long getActiveTime() {
        return activeTime;
    }

    public T getDate() {
        return data;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        long d = unit.convert(this.activeTime-System.nanoTime(),TimeUnit.NANOSECONDS);
        return d;
    }
    //按照剩余时间,进行排序
    @Override
    public int compareTo(Delayed o) {
        long d = getDelay(TimeUnit.NANOSECONDS)-o.getDelay(TimeUnit.NANOSECONDS);
        return (d==0)?0:((d>0)?1:-1);
    }
}
